package com.GroupSix.service;

import com.GroupSix.dao.DictDao;
import com.GroupSix.dao.TeacherDao;
import com.GroupSix.entity.TeacherEntity;
import com.GroupSix.entity.DictEntity;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import static com.GroupSix.tool.PasswordUtil.Md5;
import static com.GroupSix.tool.PasswordUtil.isValidPassword;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;

@Service("teacherService")
public class TeacherServiceImpl extends ServiceImpl<TeacherDao, TeacherEntity>
        implements TeacherService{

    //注入数据字典
    @Autowired
    private DictDao dictDao;

    //实现增
    @Override
    public  Integer add(TeacherEntity tch){
        tch.setTeacherPassword(Md5(tch.getTeacherPassword()));//使用MD5对密码进行加密
        if(tch.getIsActive()==null) {
            tch.setIsActive(0);
        }
        if(tch.getUpdateTime()==null) {
            tch.setUpdateTime(LocalDateTime.now());
        }
        if(tch.getCreateTime()==null){
            tch.setCreateTime(LocalDateTime.now());
        }
        return baseMapper.insert(tch);
    }

    //教师注册
    @Override
    public   String signUp(String name, Integer id, String pwd, String course){
        QueryWrapper<TeacherEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_name",name);
        queryWrapper.eq("teacher_id",id);
        if(baseMapper.selectCount(queryWrapper)>0) {
            return "注册失败，用户名或者账号已存在";
        }
        else if(!isValidPassword(pwd)) {
            return "注册失败，密码必须同时包含大小写字母，数字和至少一个特殊符号";
        }
        else{
            QueryWrapper<DictEntity> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("dict_name",course);
            TeacherEntity tch = new TeacherEntity();
            tch.setTeacherName(name);
            tch.setTeacherId(id);
            tch.setTeacherPassword(Md5(pwd));
            tch.setCourse(dictDao.selectOne(queryWrapper1).getId());
            tch.setIsActive(0);
            tch.setLoginTag(0);
            tch.setCreateTime(LocalDateTime.now());
            tch.setUpdateTime(LocalDateTime.now());
            tch.setLoginTime(LocalDateTime.now());
            baseMapper.insert(tch);
            return "注册成功";
        }
    }

    //实现删（根据账号）
    @Override
    public Integer delete(Integer id){
        QueryWrapper<TeacherEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_id",id);
        if(baseMapper.selectCount(queryWrapper)>0) {
            return baseMapper.deleteById(baseMapper.selectOne(queryWrapper).getId());
        }
        else{
            return 0;
        }
    }

    //实现改(根据id)
    @Override
    public Integer update(TeacherEntity tch){
        tch.setTeacherPassword(Md5(tch.getTeacherPassword()));//使用MD5对密码进行加密
        tch.setIsActive(0);
        tch.setLoginTag(0);
        tch.setUpdateTime(LocalDateTime.now());
        tch.setCreateTime(this.getById(tch.getId()).getUpdateTime());
        tch.setLoginTime(this.getById(tch.getId()).getLoginTime());
        return baseMapper.updateById(tch);
    }

    //分页查询
    @Override
    public IPage<TeacherEntity> list(TeacherEntity teacherEntity, Integer pageNo, Integer pageSize){
        LambdaQueryWrapper<TeacherEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TeacherEntity::getId, teacherEntity.getId());

        int currentPage = 1;
        int limit = 10;
        if (pageNo != null && pageNo > 0) {
            currentPage = pageNo;
        }
        if (pageSize != null && pageSize > 0) {
            limit = pageSize;
        }

        IPage<TeacherEntity> page = Page.of(currentPage, limit);

        return baseMapper.selectPage(page, wrapper);
    }

    //教师登录
    public String login(String name, Integer id, String pwd){
        QueryWrapper<TeacherEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_name", name);
        wrapper.eq("teacher_id", id);
        if(baseMapper.selectCount(wrapper)>0) {
            TeacherEntity tch = baseMapper.selectOne(wrapper);
            wrapper.eq("teacher_password", Md5(pwd));
            Long t = Duration.between(tch.getLoginTime(), LocalDateTime.now()).getSeconds();
            if(baseMapper.selectCount(wrapper)>0&&(tch.getLoginTag()!=3||t>=30)) {
                tch.setLoginTag(0);
                tch.setLoginTime(LocalDateTime.now());
                baseMapper.updateById(tch);
                return "登录成功";
            }
            else if(tch.getLoginTag()==3&&t<30) {
                return "请"+(30-t)+"秒后重试";
            }
            else {
                if(tch.getLoginTag()==3){
                    tch.setLoginTag(0);
                }
                int i = tch.getLoginTag();
                tch.setLoginTag(i+1);
                tch.setLoginTime(LocalDateTime.now());
                baseMapper.updateById(tch);
                return "密码错误,连续错误3次后将在30秒内无法重新登录";
            }
        }
        else{
            return "登录失败，请检查用户名或学号是否正确";
        }
    }

}
